package com.dididoctor.doctor.Activity.Usercentre.Authentication;

import android.content.Context;
import android.net.Uri;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.Bean.IHandleUpload;
import com.dididoctor.doctor.Bean.ImageInfo;
import com.dididoctor.doctor.Bean.Token;
import com.dididoctor.doctor.Http.FileResourceClient;
import com.dididoctor.doctor.Http.MyHttpResponseHandler;
import com.dididoctor.doctor.MV.BasePresenter;
import com.dididoctor.doctor.MV.BusinessClient;
import com.dididoctor.doctor.MV.IBaseView;
import com.dididoctor.doctor.MV.Response;
import com.dididoctor.doctor.Utils.ImageUtil;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

/**
 * Created by boy on 2016/4/19.
 */
public class AuthenticationPresenter extends BasePresenter {
    private IHandleUpload view = null;
    private AuthenticationView aciew;
    private Context context;
    public AuthenticationPresenter(Context context, IBaseView view) {
        super(context, view);
        this.aciew = (AuthenticationView) view;
        this.view = (IHandleUpload) view;
        this.context =context;
    }


    public void lunch(String name, String idNo,String hospName,String wtId,double  longitude,double  latitude,String cityCode) {
//        userId	String 	是	用户id
//        token	string	是
//        name	String 	是	姓名
//        idNo	String 	是	身份证号
//        hospName	string	是	就职医院
//        wtId	String 	是	职称
//        longitude	string	是	经度
//        latitude	String 	是	纬度
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("token", Token.getToken());
        params.put("name", name);
        params.put("idNo", idNo);
        params.put("hospName", hospName);
        params.put("wtId", wtId);
        params.put("longitude", longitude+"");
        params.put("latitude", latitude+"");
        params.put("cityCode", cityCode);
        BusinessClient.post(ConstantValue.certUrl, params, new MyHttpResponseHandler(aciew) {
            @Override
            public void onMyResponse(Response response) {
                if ("1".equals(response.getStatus())) {
                    aciew.AuthenticationOk(response.getStatus());
                } else {
                    aciew.AuthenticationDefeated(response.getStatus());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                aciew.showToastMessage("请求失败，请检查网络是否链接");
            }
        });

    }

    public void updateImageFromUri(Uri uri) {
        String path = ImageUtil.getImgPath(context, uri);
        updateImageFromUri(path);
    }

    /**
     * 上传图片
     *
     * @param path
     */
    public void updateImageFromUri(String path) {

        FileResourceClient.uploadToAppService(context, new MyHttpResponseHandler(view) {

            @Override
            public void onMyResponse(Response response) {
                if (!response.isSucceed()) {
                    view.showToastMessage("消息发送失败：" + response.getMsg());
                    return;
                }
                String picUrl = response.getString("picUrl");
                String picId = response.getString("picId");

                ImageInfo imageInfo = new ImageInfo();
                imageInfo.setId(picId);
                imageInfo.setType(3);
//                imageInfo.setIds(response.getString("ids"));
                imageInfo.setKey(picId);
                imageInfo.setUrl(picUrl);
                view.addImage(imageInfo);
            }

            @Override
            public void onProgress(int bytesWritten, int totalSize) {
                view.onProgress(bytesWritten, totalSize);
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                throwable.printStackTrace();
                view.onProgress(100, 100);
                view.showToastMessage("图片发送失败");
            }
        }, path);
    }

    public void delectImage(final String imageID, final int position) {
//        userId	String 	是	用户id
//        token	string	是
//        picId	string	是	资格证图片id
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("token", Token.getToken());
        params.put("picId", imageID);
        BusinessClient.post(ConstantValue.certPicDelUrl, params, new MyHttpResponseHandler(aciew) {
            @Override
            public void onMyResponse(Response response) {
                if ("1".equals(response.getStatus())) {
                    aciew.DelectImage(response.getStatus(),imageID,position);
                } else {
                    aciew.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                aciew.showToastMessage("请求失败，请检查网络是否链接");
            }
        });

    }

}
